home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / lcppb.zip / LCPP04.ZIP / POPUP.CPP < prev    next >
C/C++ Source or Header  |  1991-07-03  |  4KB  |  141 lines

  1. // popup.cpp -- Display overlapping "pop-up" windows
  2.  
  3. #include "popup.h"
  4.  
  5. main()
  6. {
  7.   int wnum;
  8.   unsigned trow, lcol, brow, rcol; // Window coordinates
  9.   winrec buffer[MAXWINDOW];        // Array of winrecs
  10.  
  11.   srand(time(NULL));   // Randomize
  12.   disp_open();         // Initialize display package
  13.  
  14. /* -- Display MAXWINDOW windows at random locations and colors */
  15.  
  16.   for (wnum = 0; wnum < MAXWINDOW; wnum++) {
  17.     trow = randrange(0, MAXROW / 2);       // Set top row
  18.     lcol = randrange(0, MAXCOL / 2);       // Set left column
  19.     brow = randrange(trow + 4, MAXROW);    // Set bottom row
  20.     rcol = randrange(lcol + 12, MAXCOL);   // Set right column
  21.     openWindow( 
  22.       &buffer[wnum],             // Pass winrec pointer
  23.       randrange(1, MAXCOLOR),    // Background color
  24.       trow, lcol, brow, rcol);   // Location
  25. //    displayText(&buffer[wnum], form("Window #%d", wnum + 1));
  26.     char string[20];
  27.     sprintf(string, "Window #%d", wnum + 1);
  28.     displayText(&buffer[wnum], string);
  29.   }
  30.  
  31. /* -- Close windows one by one */
  32.  
  33.   disp_move(24, 0);
  34.   disp_printf("Press <Space> to erase top window...");
  35.   for (wnum = MAXWINDOW - 1; wnum >= 0; wnum--)
  36.     closeTopWindow(&buffer[wnum]);
  37.   disp_move(24, 0);    // Move cursor to last row
  38.   disp_close();        // Close display package
  39. }
  40.  
  41. /* -- Display error message and quit */
  42.  
  43. void error(char *errmsg) {
  44.   disp_move(24, 0);             // Move cursor to last row
  45.   disp_close();                 // Close the display package
  46.   cout << "ERROR: " << errmsg;  // Display error message
  47.   exit(1);                      // Return to DOS
  48. }
  49.  
  50. /* -- Pause for a keypress (no message displayed) */
  51.  
  52. void pause(void)
  53. {
  54.   /*int c =*/ getch();     // Wait for any keypress
  55. }
  56.  
  57. /* -- Return random number from low ... high */
  58.  
  59. int randrange(int low, int high)
  60. {
  61.   return low + (rand() % ((high - low) + 1));
  62. }
  63.  
  64. /* -- Open a new window at these coordinates and fill with color. 
  65. Background color is ignored on monochrome displays. */
  66.  
  67. void openWindow(winrecptr wrp, int backcolor, unsigned trow, 
  68.   unsigned lcol, unsigned brow, unsigned rcol)
  69. {
  70.   int bcolor, tcolor;  // Border color, text color
  71.   unsigned bufsize;    // Saved-text buffer size
  72.   
  73. /* -- Determine color to use for background and foreground */
  74.  
  75.   if (disp_getmode() == MONOCHROME) {
  76.     bcolor = DISP_REVERSEVIDEO;         // Monochrome display
  77.     tcolor = bcolor;
  78.   } else {
  79.     bcolor = (backcolor << 4) + WHITE;  // Color display
  80.     tcolor = bcolor - WHITE;
  81.   }
  82.  
  83. /* -- Calculate number of bytes needed to save text behind window */
  84.  
  85.   bufsize = 
  86.   ((brow - trow + 1) * (rcol - lcol + 1)) * sizeof(unsigned);
  87.  
  88.   wrp->bufptr = malloc(bufsize);  // Reserve space for buffer
  89.   if (wrp->bufptr == NULL)        // If that fails, return
  90.     return;                      //  leaving bufptr == NULL
  91.  
  92. /* -- Save text behind window in buffer just created */
  93.  
  94.   disp_peekbox(wrp->bufptr, trow, lcol, brow, rcol);
  95.   wrp->trow = trow;   // Save coordinates and color, too
  96.   wrp->lcol = lcol;
  97.   wrp->brow = brow;
  98.   wrp->rcol = rcol;
  99.   wrp->attribute = tcolor;    // Text color
  100.  
  101. /* -- Pause and then display the window */
  102.  
  103. //  msleep(250);   // Pause for 1/4 second
  104.   delay(250);   // Pause for 1/4 second
  105.   disp_fillbox(((bcolor * 256) + ' '), trow, lcol, brow, rcol);
  106.   disp_box(0, bcolor, trow, lcol, brow, rcol);
  107. }
  108.  
  109. /* -- Display some text inside this window (assumes that window is
  110. frontmost on display) */
  111.  
  112. void displayText(winrecptr wrp, char *message)
  113. {
  114.   if (wrp->bufptr == NULL) return;  // Exit if window not open
  115.   disp_move(wrp->trow + 2, wrp->lcol + 2);
  116.   disp_setattr(wrp->attribute);
  117.   disp_printf(message);
  118.   disp_setattr(DISP_NORMAL);
  119. }
  120.  
  121. /* -- Close window using winrec struct addressed by wrp. Assumes that
  122. this window is frontmost on display. */
  123.  
  124. void closeTopWindow(winrecptr wrp)
  125. {
  126.   if (wrp->bufptr == NULL) return;  // Exit if window not open
  127.   pause();
  128.   disp_pokebox(
  129.     wrp->bufptr, wrp->trow, wrp->lcol, wrp->brow, wrp->rcol);
  130.   free(wrp->bufptr);   // Dispose buffer
  131.   wrp->bufptr = NULL;  // Prevent further use of buffer
  132. }
  133.   
  134.  
  135. // Copyright (c) 1990 by Tom Swan. All rights reserved
  136. // Revision 1.00    Date: 08/30/1990   Time: 09:40 pm
  137.  
  138. // Revision 1.01    Date: 07/03/1991   Time: 04:00 pm
  139. // Converted for Borland C++ 2.0
  140.  
  141.